package org.ndot.java.util.concurrent.bank.ThreadPool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.ndot.java.util.concurrent.bank.BankParam;
import org.ndot.java.util.concurrent.bank.Consumer;

/**
 * @公司名称: 小蚂蚁学堂
 * 
 * @项目名称: temp
 * 
 * @类名名称: org.ndot.bank.BankOfChina.java
 * 
 * @作者姓名: SunJincheng
 * 
 * @软件版本: v1.0
 * 
 * @创建日期: 2010-4-13下午04:26:27
 */
public class BankOfChinaPool {

	private BankWin winComm;
	private BankWin winRap;
	private BankWin winVip;

	private ThreadPoolExecutor commPool;
	private ThreadPoolExecutor rapPool;
	private ThreadPoolExecutor vipPool;

	private List<Consumer> commList;
	private List<Consumer> rapList;
	private List<Consumer> vipList;

	@SuppressWarnings("unchecked")
	public BankOfChinaPool() {

		commList = new ArrayList<Consumer>();
		rapList = new ArrayList<Consumer>();
		vipList = new ArrayList<Consumer>();

		commPool = new ThreadPoolExecutor(4, 4, 8, TimeUnit.HOURS,
				new LinkedBlockingQueue<Runnable>(),
				new ThreadPoolExecutor.DiscardPolicy());

		rapPool = new ThreadPoolExecutor(1, 1, 8, TimeUnit.HOURS,
				new LinkedBlockingQueue<Runnable>(),
				new ThreadPoolExecutor.DiscardPolicy());

		vipPool = new ThreadPoolExecutor(1, 1, 8, TimeUnit.HOURS,
				new LinkedBlockingQueue<Runnable>(),
				new ThreadPoolExecutor.DiscardPolicy());

		winComm = new BankWin(commPool, commList);
		winComm.setName("win_comm");
		winRap = new BankWin(rapPool, rapList, commList);
		winRap.setName("win_rap");
		winVip = new BankWin(vipPool, vipList, commList);
		winVip.setName("win_vip");
	}

	public void start() {
		System.out.println("中国银行开始营业.......");

		this.winComm.start();
		this.winRap.start();
		this.winVip.start();
	}

	/**
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		try {
			BankOfChinaPool bank = new BankOfChinaPool();

			GenConsumer genComm = new GenConsumer(bank.commList, 1);
			GenConsumer genRap = new GenConsumer(bank.rapList, 3);
			GenConsumer genVip = new GenConsumer(bank.vipList, 6);

			genComm.start();
			genRap.start();
			genVip.start();

			bank.start();

			Thread.sleep(60000);

			genComm.setTag(false);
			genRap.setTag(false);
			genVip.setTag(false);

			bank.winComm.setEnd(true);

			bank.winRap.setEnd(true);
			bank.winVip.setEnd(true);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

/**
 * 
 * @公司:小蚂蚁学堂
 * 
 * @日期:2010-4-14上午09:14:47
 * 
 * @项目名称:J2SE_Study
 * 
 * @类名称:org.ndot.java.util.concurrent.bank.ThreadPool.BankOfChina.java
 * 
 * @作者:SunJincheng
 * 
 * @版本:1.0
 * 
 */
class GenConsumer extends Thread {
	private List<Consumer> consumerList;
	private int speed = 6;
	private boolean tag = true;
	private int i = 1;
	private String msg = "";

	public GenConsumer(List<Consumer> consumerList, int speed) {
		this.consumerList = consumerList;
		this.speed = speed;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		try {
			switch (speed) {
			case 6:
				msg = BankParam.printInfo(BankParam.TYPE_VIP);
				while (tag) {
					consumerList.add(new Consumer(i++, BankParam.TYPE_VIP,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);
				}
				System.err
						.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			case 3:
				msg = BankParam.printInfo(BankParam.TYPE_RAP);
				while (tag) {
					consumerList.add(new Consumer(i++, BankParam.TYPE_RAP,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);
				}
				System.err
						.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			case 1:
				msg = BankParam.printInfo(BankParam.TYPE_COMM);
				while (tag) {
					consumerList.add(new Consumer(i++, BankParam.TYPE_COMM,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);

				}
				System.err
						.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isTag() {
		return tag;
	}

	public void setTag(boolean tag) {
		this.tag = tag;

	}

	private int getDoTime(int type) {
		double tem = (Math.random() * BankParam.MAX_TIME);
		tem = tem > BankParam.MIN_TIME ? tem : BankParam.MIN_TIME;
		if (3 == type)
			return BankParam.MIN_TIME;
		return (int) tem;
	}
}
